/*
* Copyright 2018 Shanxi Lu'an Taihang Laihui Net Tech co.,LTD.
*/
package com.laihuichuxing.pool.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.laihuichuxing.common.config.CacheConfig;
import com.laihuichuxing.pool.UserRpPool;
import com.laihuichuxing.pool.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Description:
 *
 * @author: Qinxuefeng
 * @since: 1.0
 * Create Date Time: 2018/10/10 17:20
 * Update Date Time:
 */
@Component
public class UserRpPoolImpl implements UserRpPool {
    @Autowired
    StringRedisTemplate redisTemplate;
    @Override
    public void offer(UserEntity userEntity) {
        redisTemplate.opsForValue().set(CacheConfig.USERRP+userEntity.getMobile(),userEntity.toString());
    }

    @Override
    public UserEntity take(UserEntity userEntity) {
        UserEntity user = new UserEntity();
        String str=redisTemplate.opsForValue().get(CacheConfig.USERRP+userEntity.getMobile());
        if(!Strings.isNullOrEmpty(str)){
            Map<String,String> map = Splitter.on(",").withKeyValueSeparator("=").split(str);
            user = JSON.parseObject(JSON.toJSONString(map), UserEntity.class);
        }
        return user;
    }

}
